<template>
  <van-button
   :icon=" value ? 'star' : 'star-o' "
   :class="{
     collected: value
   }"
   @click="onCollect"
   :loading="loading"
   />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'collectArticle',
  data () {
    return {
      loading: false
    }
  },

  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },

  created () {

  },

  methods: {
    async onCollect () {
      this.loading = true
      try {
        if (this.value) {
          const { data } = await deleteCollect(this.articleId)
          console.log(data)
        } else {
          const { data } = await addCollect(this.articleId)
          console.log(data)
        }
        this.$emit('input', !this.value)
        this.$toast.success(!this.value ? '收藏成功' : '取消收藏')
      } catch (err) {
        this.$toast.fail('操作失败')
      }
      this.loading = false
    }
  }
}
</script>

<style scoped lang='less'>
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>
